import store from '@/store/index'
import Vue from 'vue';
import data from "./test.js"
import VueI18n from 'vue-i18n'


const globalRequest = (api, method, options) => {

	const i18n = new VueI18n({
		locale: store.state.lang ? store.state.lang.toString() : 'zh', // 语言标识
		messages: {
			'zh': require('@/common/lang/zh.json'), // 中文简体语言包
			'en': require('@/common/lang/en.json'), // 英语
		},
		silentTranslationWarn: true,
	})
	// console.log(111, i18n.t('t')['请检查网络']);



	let loadingTimer = null;
	const option = {
		loading: true, //是否显示loading
		delayed: true, //是否延时显示loading
		loading_text: 'loading', //是否延时显示loading
		params: {}
	}
	if (options) {
		if (options.loading !== undefined) option.loading = options.loading
		if (options.delayed !== undefined) option.delayed = options.delayed
		if (options.loading_text !== undefined) option.loading_text = options.loading_text
		if (options.params !== undefined) option.params = options.params
	}


	let request_url = Vue.prototype.$config.baseURL + api;
	if (/^http/.test(api)) {
		request_url = api
	} else if (/^test/.test(api)) {
		return new Promise((resolve, reject) => {
			setTimeout(() => {
				if (options && options.params && options.params.page && options.params.page >= 3) {
					data.data.next = 0
				} else {
					data.data.next = 1
				}
				resolve(data.data)
			}, 300)
		})
	}
	const headers = {
		// "Content-Type": 'application/x-www-form-urlencoded',
		"token": uni.getStorageSync('token') || '', //token
	}


	if (option.loading && !option.delayed) {
		// 立即显示loading
		store.commit("loadingShow", option.loading_text)
	} else if (option.loading && option.delayed) {
		// 延时显示loading
		loadingTimer = setTimeout(() => {
			console.log("显示弹窗");
			store.commit("loadingShow", option.loading_text)
		}, 1000)
	}

	return new Promise(function(resolve, reject) {
		uni.request({
			url: request_url,
			method: method,
			data: option.params,
			dataType: 'json',
			withCredenttials: true,
			header: headers,
			success: (res) => {
				clearTimeout(loadingTimer)
				store.commit("loadingHide") //隐藏loading
				if (res.statusCode == 200) { //浏览器200
					switch (res.data.code) {
						case 1:
							resolve(res.data.data)
							break;
						case -1:
							store.commit("loginout");
							uni.reLaunch({
								url:"/pages/login/login-pass"
							})
							reject(res.data);
							break;
						default:
							reject(res.data) // 返回失败
							break;
					}
				} else if (res.statusCode == 401) {
					store.commit("loginout");
					uni.reLaunch({
						url:"/pages/login/login-pass"
					})
					reject({ message: "" });
				} else {
					reject({ message: "" })
				}
			},
			fail: (err) => {
				clearInterval(loadingTimer)
				store.commit("loadingHide") //隐藏loading
				if (store.state.network) {
					uni.showModal({
						content: i18n ? i18n.t('请检查网络') : '请检查网络',
						showCancel: false,
						confirmColor: "#69B7FF"
					});
					store.commit('setNetwork', false)

					setTimeout(() => {
						store.commit('setNetwork', true)
					}, 5000)
				}
				reject({ message: "" })
			}
		})
	})
}



// import request from '@/common/request.js'
const api = {
	post: (api_path, options) => globalRequest(api_path, 'POST', options),
	get: (api_path, options) => globalRequest(api_path, 'GET', options)
}

export default api